package cn.cnsy123.shorturls.api.controller.business;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.cnsy123.shorturls.business.service.ShortUrlAddrService;

@Controller
public class ShortUrlQueryController {
    
    private static final Logger logger = LoggerFactory.getLogger(ShortUrlQueryController.class);
    
    @Autowired
    private ShortUrlAddrService shortUrlAddrService;

    @RequestMapping(value = "/tt/{shortUrl}",method = RequestMethod.GET)
    public ModelAndView convertSourceAddr(@PathVariable(value = "shortUrl",required = true)String shortUrl) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("redirectHttpAddr");
        try {
            String sourceAddr = shortUrlAddrService.queryShortAddressItem(shortUrl);
            if(StringUtils.isNotBlank(sourceAddr)) {
                modelAndView.addObject("sourceAddr", sourceAddr);
            }
        } catch (Exception e) {
            logger.error("查询短地址:{"+shortUrl+"}对应的原始地址出错",e);
        }
        return modelAndView;
    }
}
